<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/orderset.css">

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>知识库
            <small>运动库</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>知识库</el-breadcrumb-item>
            <el-breadcrumb-item>运动库</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

    <el-card class="box-card">
        <div class="boxMain">
            <el-button style="margin-bottom: 20px;margin-right: 20px" type="primary" @click="downloadTemplate()">
                模板下载
            </el-button>
            <el-upload action="/knowledgeBase/upload"
                       name="excelFile"
                       :show-file-list="false"
                       :on-success="handleSuccess"
                       :before-upload="beforeUpload">
                <el-button type="primary">上传文件</el-button>
            </el-upload>
        </div>
        <div>
            操作说明：请点击"模板下载"按钮获取模板文件，在模板文件中录入预约设置数据后点击"上传文件"按钮上传模板文件。
        </div>
    </el-card>

    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="名称/助记码" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="pagination.currentPage = 1 ; findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="helpCode" label="运动助记码" align="center"></el-table-column>
                <el-table-column prop="name" label="运动名称" align="center"></el-table-column>
                <el-table-column prop="frequency" label="运动次数" align="center"></el-table-column>
                <el-table-column prop="duration" label="运动时间" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增运动" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="运动信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="运动助记码">
                                                <el-input v-model="formData.helpCode"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="运动名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="运动次数">
                                                <el-input v-model="formData.frequency" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="运动时间">
                                                <el-input v-model="formData.duration" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 修改标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑套餐" :visible.sync="dialogFormVisibleEdit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="运动助记码">
                                                <el-input v-model="formData.helpCode"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="运动名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="运动次数">
                                                <el-input v-model="formData.frequency" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="运动时间">
                                                <el-input v-model="formData.duration" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handlerEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            // tableData: [],//添加表单窗口中检查组列表数据
            // checkgroupIds: [],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisibleEdit: false //控制修改窗口显示/隐藏
        },
        created(){
            this.findPage();
        },
        methods: {
            //下载模板文件
            downloadTemplate() {
                window.location.href = "../template/sportlist_template.xlsx";
            },
            //上传之前进行文件格式校验
            beforeUpload(file) {
                const isXLS = file.type === 'application/vnd.ms-excel';
                if (isXLS) {
                    return true;
                }
                const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                if (isXLSX) {
                    return true;
                }
                this.$message.error('上传文件只能是xls或者xlsx格式!');
                return false;
            },
            //上传成功提示
            handleSuccess(response, file) {
                if (response.flag) {
                    this.findPage();
                    this.$message({
                        message: response.message,
                        type: 'success'
                    });
                } else {
                    this.$message.error(response.message);
                }
                console.log(response, file, fileList);
            },
            //初始化页面
            findPage(){
                let url = "/knowledgeBase/findPage";
                let params={
                    pageSize : this.pagination.pageSize,
                    currentPage : this.pagination.currentPage,
                    queryString : this.pagination.queryString
                };
                axios.get(url,{params:params}).then(response => {
                    if(response.data.flag){
                        this.dataList = response.data.data.list;
                        this.pagination.total=response.data.data.total;
                        /*this.$message({
                            message:response.data.message,
                            type:'success'
                        });*/
                    }else {
                        this.$message.error(response.data.message);
                    }
                });
            },
            //显示新增框
            handleCreate(){
                //初始化新增框
                this.formData={};
                this.dialogFormVisible = true;
            },
            //提交新增数据
            handleAdd(){
                let url = "/knowledgeBase/addOneSport";
                axios.post(url,this.formData).then(response => {
                    if(response.data.flag){
                        this.$message({
                            message:response.data.message,
                            type:'success'
                        });
                    }else {
                        this.$message.error(response.data.message);
                    }
                    this.dialogFormVisible=false;
                    this.findPage();
                });
            },
            //开启并初始化编辑框
            handleUpdate(row){
                this.dialogFormVisibleEdit=true;
                let url = "/knowledgeBase/findById/"+row.id;
                axios.get(url).then(response => {
                    if(response.data.flag){
                        this.formData=response.data.data;
                    }else {
                        this.$message.error(response.data.message);
                    }
                });
            },
            //提交修改
            handlerEdit(){
                let url = "/knowledgeBase/update";
                axios.put(url,this.formData).then(response => {
                    if(response.data.flag){
                        this.$message.success(response.data.message);
                    }else {
                        this.$message.error(response.data.message);
                    }
                }).finally(() => {
                    this.dialogFormVisibleEdit=false;
                    this.findPage();
                });
            },
            handleDelete(row){
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = "/knowledgeBase/deleteById/"+row.id;
                    axios.delete(url).then(response => {
                        if(response.data.flag){
                            this.$message.success(response.data.message);
                            this.findPage();
                        }else {
                            this.$message.error(response.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        }
    })
</script>
</html>